<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

include_once("impl/ImageImpl.php");
include_once("impl/ContentImpl.php");
include_once("impl/CategoryImpl.php");
include_once("impl/CelebrityImpl.php");
include_once("impl/TagImpl.php");

include_once("utils/FileUtils.php");				//--> Files
include_once("utils/ImageUtils.php");

	
$contentImpl= new ContentImpl(DB_TAG_MM);
$oCatImpl 	= new CategoryImpl(DB_TAG_MM);
$oCeleImpl 	= new CelebrityImpl(DB_TAG_MM);
$imgImpl	= new ImageImpl(DB_TAG_MM);
$tagImpl	= new TagImpl(DB_TAG_MM);


$error_message 	= '';

$name		= '';
$summary	= '';
$status		= '1';
$recommend	= '0';

$arrCelebrity = array();

if(isset($_POST['searchCelebrity'])){
	$name			= trim($_POST['name']);
	$description	= trim($_POST['description']);
	$status			= $_POST['status'];
	
	$catIds			= $_POST['catIds'];// array
	
	$celebrity		= addslashes($_POST['celebrity']);
	
	$where = "WHERE name like '%$celebrity%'";
	$arrCelebrity	= $oCeleImpl->getList($where);
}

if(isset($_POST['Submit'])){
	$name			= trim($_POST['name']);
	$tags			= trim($_POST['tags']);
	
	$comments		= trim($_POST['comments']);
	$description	= trim($_POST['description']);
	
	$recommend		= $_POST['recommend'];
	$status			= $_POST['status'];
	
	$catIds			= $_POST['catIds'];// array
	$celebrityId	= $_POST['celebrityId'];
	
	$dto = new ContentDto();
				
	$dto->name 			= $name;
	$dto->comments 		= $comments;
	$dto->description	= $description;
	$dto->status		= $status;
	$dto->recommend		= $recommend;
	
	$dto->r_imageCount	= 0;
	
	$id = $contentImpl->add($dto);
	if($id){
		$contentImpl->addCategories($id, $catIds);
		
		if(!empty($celebrityId)){
			$contentImpl->addCelebrity($id, $celebrityId);
		}
		
		$tagIds = $tagImpl->addTags($tags);
		if(is_array($tagIds)){
			foreach($tagIds as $tagId)
				$contentImpl->addTag($id, $tagId);
		}
		
		// add images
		
		$arrImages = array();
		$blacklist = array(".php", ".phtml", ".php3", ".php4");
		
		foreach ($_FILES["image"]["error"] as $key => $error) {
	    	if ($error == UPLOAD_ERR_OK) {
				foreach ($blacklist as $item) {
					if(preg_match("/$item\$/i", $_FILES['image']['name'][$key])) {
					 	$error_message = "only support gif and jpg format.";
					}
				}
				
				if(empty($error_message)){
					$imageinfo = getimagesize($_FILES['image']['tmp_name'][$key]); 
		
					if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { 
						$error_message = "only support gif and jpg format.";
					}else if($file_size >8000000)
						$error_message = 'Your image more than 8000 kb('.ceil($file_size/1024).'kb)';
					else{
						/*-- get ext image file name --*/
						$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
						$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
						
						$arrImage = array();
						$arrImage['name'] 		= $_FILES['image']['name'][$key];
						$arrImage['tmp_name'] 	= $_FILES['image']['tmp_name'][$key];
						$arrImage['file_size']	= filesize($_FILES['image']['tmp_name'][$key]);
						
						$arrImage['width']		= $imageinfo[0];
						$arrImage['height']		= $imageinfo[1];
						
						$arrImage['md5']		= md5(file_get_contents($_FILES['image']['tmp_name'][$key]));
						$arrImage['extName']	= $image_extend_name;
						
						$arrImages[] 			= $arrImage;
					}
				}		
	    	}
	    	
	    	if(!empty($error_message)){
	    		break;
	    	}
		}
		
		foreach ($arrImages as $arrImg) {
			$imgdto = $imgImpl->getByMd5($arrImg['md5']);
			if(is_object($imgdto)){			
	//			$dto->name 			= $name;
	//			$dto->description	= $description;
	//			
	//			$isOk = $imgImpl->edit($dto);
	//			
	//			if($isOk)
	//				$error_message = "Image existed, update success.";
	//			else
				$contentImpl->addImage($id, $imgdto->id);
				
				$error_message = "Image existed, Use it. Image md5 " . $arrImg['md5'];
			}else{	
				$imgdto = new ImageDto();
				
				$imgdto->width			= $arrImg['width'];
				$imgdto->height			= $arrImg['height'];
				$imgdto->extName		= $arrImg['extName'];
				$imgdto->fileSize		= $arrImg['file_size'];
				$imgdto->md5			= $arrImg['md5'];
				
				$imgdto->name 			= $name;
				$imgdto->description	= '';
				$imgdto->status			= $status;
				$imgdto->level			= '10';
				$imgdto->isAnimated	= ImageUtils::isAnimatedGif($arrImg['tmp_name']);
				
				$imageid = $imgImpl->add($imgdto);
				
				if($imageid > 0){
					$contentImpl->addImage($id, $imageid);
									
					$imageName = $imageid . '.' . strtolower($arrImg['extName']);
					
					if (!move_uploaded_file($arrImg['tmp_name'], FileUtils::getFullPathByID(IMAGE_PATH_CONTENT, $imageid, $imageName))) {//-- save image
						$error_message = "add new image failure.";
					}else
						$error_message = "add new image success.";	
						
					$imgImpl->delTags($imageid);

					if(is_array($tagIds)){
						foreach($tagIds as $tagId)
							$imgImpl->addTag($imageid, $tagId);
					}				
				}else
					$error_message = "add new image failure.";
			}
		}
		
		$dto->r_imageCount	= $contentImpl->getImageCount($id);
		
		$isOk = $contentImpl->edit($dto);
		
		$error_message .= ' add succeeded.';
	}else 
		$error_message .= ' add failure.';
}

$arrCelebrity	= $oCeleImpl->getList();
$catLeaf 		= $oCatImpl->getLeafList();

$arrStatus 		= array('1' => 'Online', '0' => 'Offline');
$arrRecommend 	= array('1' => 'Yes', '0' => 'No');


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;

$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('status', $status);

$smarty->assign('arrRecommend', $arrRecommend);
$smarty->assign('recommend', $recommend);

$smarty->assign('tags', $tags);
$smarty->assign('name', $name);

$smarty->assign('description', $description);
$smarty->assign('comments', $comments);

$smarty->assign('catLeaf', $catLeaf);
$smarty->assign('catIds', $catIds);

$smarty->assign('arrCelebrity', $arrCelebrity);
$smarty->assign('celebrityId', $celebrityId);

$smarty->display('listContent_Add.htm');
?>
